package no.sachmuld.methodcombination.examples.reflection.aopstyle.request;

import no.sachmuld.methodcombination.api.CrosscuttingBehaviour;
import no.sachmuld.methodcombination.api.MethodInvocation;
import no.sachmuld.methodcombination.api.BehaviourChain;
import no.sachmuld.methodcombination.api.reflection.Crosscutting;

/**
 *
 */
public class Foo {

    private BehaviourChain<CrosscuttingBehaviour> chain = new BehaviourChain<>();

    protected Foo() {
        with(new FooBehaviour() {
            @Override
            public void processPendingRequests(MethodInvocation<Void> methodInvocation) {
                for (int i = 0; i < 3; i++) {
                    Foo.this.processPendingRequest(i);
                }
            }

            @Override
            public void processPendingRequest(int i, MethodInvocation<Void> methodInvocation) {
                System.out.println("Processing request " + i);
            }
        });
    }

    final public void processPendingRequests() {
        new Crosscutting<Void>().build(FooBehaviour.class, Foo.class, "processPendingRequests").execute(this, chain);
    }

    @Transactional
    final public void processPendingRequest(int i) {
        new Crosscutting<Void>().build(FooBehaviour.class, Foo.class, "processPendingRequest", Integer.TYPE).execute(this, chain, i);
    }

    final protected Foo with(CrosscuttingBehaviour... behaviours) {
        chain.addAll(behaviours);
        return this;
    }

}
